/*
 *@Author: 张楚
 *@Date: 2022-01-13 15:56:36
 *@Description: 时间范围选择器
*/

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class DateRangPage extends StatefulWidget {
  const DateRangPage({Key key}) : super(key: key);
  @override
  _DateRangPageState createState() => _DateRangPageState();
}

class _DateRangPageState extends State<DateRangPage> {
  String _dateSelectText;
  @override
  Widget build(BuildContext context) {
//CustomWidgets.appBarWhite(context, '会议详情'),
    return Scaffold(
      appBar: AppBar(
        title: Text('时间范围选择器'),
      ),
      body: Container(
        child: Center(
          child: Text(_dateSelectText ?? 'd'),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          showDateSelect();
        },
        child: Icon(Icons.select_all),
      ),
    );
  }

  void showDateSelect() async {
    //获取当前的时间
    DateTime start = DateTime.now();
    //在当前的时间上多添加4天
    DateTime end = DateTime(start.year, start.month, start.day + 4);

    //显示时间选择器
    DateTimeRange selectTimeRange = await showDateRangePicker(
      //语言环境
      locale: Locale("zh", "CH"),
      context: context,
      //开始时间
      firstDate: DateTime.now(),
      //结束时间
      lastDate: DateTime(2022, 5),
      initialEntryMode: DatePickerEntryMode.calendar,
      useRootNavigator: false,
      // cancelText: "取消",
      // confirmText: "确定",
      //初始的时间范围选择
      // initialDateRange: DateTimeRange(start: start, end: end)
    );
    //结果
    if (selectTimeRange != null) {
      _dateSelectText = selectTimeRange.duration.inDays.toString();
    }
    //选择结果中的开始时间
    // DateTime selectStart = selectTimeRange.start;
    // //选择结果中的结束时间
    // DateTime selectEnd = selectTimeRange.end;

    setState(() {});
  }
}
